package com.gp.controller.system;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.gp.common.result.JsonResult;
import com.gp.common.utils.StringUtil;
import com.gp.controller.BaseController;
import com.gp.model.dto.Count;
import com.gp.model.entity.AccessLog;
import com.gp.model.entity.BaseLoginLogs;
import com.gp.service.IBaseAccessLogsService;
import com.gp.service.IBaseLoginLogsService;

/**
 * @author: 弓成龙
 * @date  : 2018年3月16日
 * @email : gcx909109@sina.cn
 * @description: 
 */
@RestController
@RequestMapping(value="/log")
public class LogController extends BaseController{
      
  // 登录日志 
  @Autowired
  IBaseLoginLogsService loginLogService; 
  // 访问日志  
  @Autowired
  IBaseAccessLogsService accesslogService;
  
  /**
   * 查询登录日志
   * @param page
   * @return
   */
  @RequestMapping(value = "/listLoginLog",method = RequestMethod.GET)
  public JsonResult listLoginLogs(Page<BaseLoginLogs> page,@RequestParam(value="isSearch",required=false) Integer isSearch) {
    if(isSearch!=null && isSearch==1) {
      Wrapper<BaseLoginLogs> wapper = new EntityWrapper<>();
      Map<String, Object> condition = page.getCondition();
      wapper.like("username", (String)condition.get("username"));
      page.setCondition(null);
      Page<BaseLoginLogs> page2 = loginLogService.selectPage(page,wapper);
      page2.setCondition(condition);
      return new JsonResult("检索成功",page2); 
    }else {
       Page<BaseLoginLogs> result = loginLogService.selectPage(page);
    }
    return new JsonResult("检索成功",page);
  }
  
  /**
   * 删除一条登录日志
   * @param page
   * @return
   */
  @RequestMapping(value = "/delLoginlog/{id}",method = RequestMethod.POST)
  public JsonResult delLoginlog(@PathVariable(value="id") String id) {
     loginLogService.deleteById(Long.parseLong(id));
    return renderSuccess("删除成功");
  }
  
  /**
   * 查询访问日志
   * @param page
   * @return
   */
  @RequestMapping(value = "/listAccessLog",method = RequestMethod.GET)
  public JsonResult listAccessLogs(Page<AccessLog> page) {
    Page<AccessLog> pages = accesslogService.selectPage(page);
    return renderSuccess("查询成功", pages);
  }
   
  /**
   * 查询接口访问量
   * @param page
   * @return
   */
  @RequestMapping(value = "/interCount",method = RequestMethod.GET)
  public JsonResult listInterAccessCount(Page<Count> page) {
    Page<Count> pages = accesslogService.listInterAccessCount(page);
    return renderSuccess("查询成功", pages);
  }
  
  /**
   * 统计一个页面的pv
   * @param url
   * @param starttime
   * @param endtime
   * @return
   */
  @RequestMapping(value = "/pv",method = RequestMethod.GET)
  public JsonResult listPV(String url,String starttime,String endtime) {
    if(StringUtil.validDate(starttime,endtime)) {
      return renderError("参数不合法");
    }
    Page<Count> pages = accesslogService.listPV(url,starttime,endtime);
    return renderSuccess("查询成功", pages);
  }

  /**
   * 统计一个页面的uv
   * @param url
   * @param starttime
   * @param endtime
   * @return
   */
  @RequestMapping(value = "/uv",method = RequestMethod.GET)
   public JsonResult listUV(String url,String starttime,String endtime) {
    if(StringUtil.validDate(starttime,endtime)) {
      return renderError("参数不合法");
    }
    Page<Count> pages = accesslogService.listUV(url,starttime,endtime);
    return renderSuccess("查询成功", pages);
  }
  
  /**
   * 统计活跃相关数据
   * @return
   */
  @RequestMapping(value = "/active",method = RequestMethod.GET)
  public JsonResult listActive() {
    Page<Count> pages = accesslogService.listActive();
    return renderSuccess("查询成功", pages);
  }

  /**
   * 统计留存，转化率
   * @return
   */
  @RequestMapping(value = "/keepOrTurn",method = RequestMethod.GET)
  public JsonResult listKeepOrTurn() {
    Page<Count> pages = accesslogService.listKeepOrTurn();
    return renderSuccess("查询成功", pages);
  }
}

